package com.nosql.db.core.modul;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

public class Document implements Serializable {
     private  String id;
     private  String collection;
     private Map<String,Object> data;
     private long timestamp;
    private boolean tombstone; // 墓碑标记
     // 构造方法
     public Document( String collection, Map<String,Object> data) {
    	 this.collection = collection;
    	 this.data = new HashMap<>(data);
    	 this.timestamp = System.currentTimeMillis();
     }

     public Document(String collection, Map<String, Object> data, long timestamp){
         this.collection=collection;
         this.data = new HashMap<>(data);
         this.timestamp = timestamp;
     }

    public Document(String id, String collection, Map<String,Object> data, long timestamp) {
        this.id = id;
        this.collection = collection;
        this.data = new HashMap<>(data);
        this.timestamp = timestamp;
    }
     // 添加数据
     public void put (String key, Object value) {
          validateValueType(value);
           data.put(key, value);
     }
    public void setData(Map<String, Object> data) {
        this.data.clear();
        data.forEach((k, v) -> {
            if (!k.startsWith("_")) { // 过滤元数据字段
                this.data.put(k, v);
            }
        });
    }
      // 验证数据类型
     private void validateValueType( Object value){
         if (!(value instanceof String) &&
                 !(value instanceof Integer) &&
                 !(value instanceof Double) &&
                 !(value instanceof Boolean) &&
                 !(value instanceof List) &&
                 !(value instanceof Map) &&
                 !(value instanceof byte[])) {
             throw new IllegalArgumentException("不支持的数据类型: " + value.getClass());
         }
     }

    // 创建墓碑文档的工厂方法
    public static Document createTombstone(String id, String collection) {
        Document doc = new Document(collection, Collections.emptyMap());
        doc.setId(id);
        doc.setTombstone(true);
        return doc;
    }

    public void markAsTombstone() {
        this.tombstone = true;
        this.data.clear(); // 清除数据
    }

    public boolean isTombstone() {
        return tombstone;
    }

    public void setTombstone(boolean tombstone) {
        this.tombstone = tombstone;
    }

    public String getId() {
        return id;
    }

    public Document setId(String id) {
        this.id = id;
        return this;
    }


    public String getCollection() {
        return collection;
    }

    public void setCollection(String collection) {
        this.collection = collection;
    }

    public Map<String, Object> getData() {
        return data;
    }


    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
    // 新增合并更新方法
    public void mergeData(Map<String, Object> newData) {
        newData.forEach((key, value) -> {
            if (!key.startsWith("_") && !key.equalsIgnoreCase("id")) {
                this.data.put(key, value);
            }
        });
        this.timestamp = System.currentTimeMillis();
    }

    @Override
    public String toString() {
        return "Document{" +
                "id='" + id + '\'' +
                ", collection='" + collection + '\'' +
                ", version=" + timestamp +
                ", data=" + data.entrySet().stream()
                .filter(e -> !e.getKey().startsWith("_"))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) +
                '}';
    }
}
